<!doctype html>
<html>
<head>
	<meta charset="utf-8"><meta name="format-detection" content="telephone=no">
	<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
	<title>机构</title>
	<link rel="stylesheet" type="text/css" href="../../lib/css/skin.css"/>
</head>
<body>
<div id="root">
	<div class="app_warp">
		<div class="app_head">
			<span class="left back"></span>
			<span class="name">订单支付及确认</span>
		</div>
		<div id="mechanism_order_view" class="app_main">

			<div class="nocontent animated fadeInUp">
				<img src="../../lib/img/loading.gif" class="minimize" alt="">
				<div class="text minimize">
					准备数据
				</div>
			</div>
		</div>

		<a href="javascript:;" class="skin_btn" style="display: none" tapmode onclick="payThisOrder()">
			立即支付
		</a>

	</div>
</div>

<script id="mechanism_order_view_template" type="text/html">
	<div class="animated fadeInUp">
		<div class="space"></div>
		<ul class="order_view">

			<li>
				<span class="name">
					约课详情
				</span>
			</li>
			<li>
				<span class="key">
					课程名称
				</span>
				<span class="val">
					<%:=data.lname%>
				</span>
			</li>
			<li>
				<span class="key">
					课程费用
				</span>
				<span class="val">
					<%:=data.price%>元
				</span>
			</li>

			<li class="space"></li>

			<li>
				<span class="name">
					机构
				</span>
			</li>
			<li class="col">
				<img src="<%:=img%>" data_src="<%:=data.mavatar%>" alt="" class="order_cover imgcache">
				<span class="tit">
					<%:=data.mname%>
				</span>
				<span class="intro">
					<%:=data.area%> / <% for(var i in data.direction){ %><%:=data.direction[i]%> <%}%>
				</span>
			</li>

			<li class="space"></li>

			<li>
				<span class="name">
					学生
				</span>
			</li>

			<li class="col">
				<img src="<%:=img%>" data_src="<%:=data.studentAvatar%>" alt="" class="order_cover imgcache">
				<span class="tit tit_c">
					<%:=data.studentName%>
				</span>
			</li>

			<li>
				<span class="name">
					支付方式
				</span>
			</li>

			<li class="col" style="padding: 0;min-height:auto;">
				<img src="../../lib/img/pay.png" alt="">
			</li>

			<li class="space"></li>

			<li>
				<span class="key">
					咨询客服
				</span>
				<span class="val">
					<a class="tel" href="tel:<%:=data.tel%>"><%:=data.tel%></a>
				</span>
			</li>
		</ul>
	</div>

</script>

<script>
	var
		//跳转订单列表的动作
		getOverPage = function () {
			var winName = api.winName;
			_$.sleep(function () {
				api.execScript({
					name: 'index',
					frameName: 'user',
					script: 'getPage("dingdan")'
				})
				_$.sleep(function () {
					api.closeWin({
						name: winName
					})
				}, 500)
			}, 2000)
		},
		//下单结束的流程判断
		payOver = function (ret, err) {
			if (ret.status) {
				api.toast({
					msg: '支付成功'
				})
				getOverPage();
			} else {
				console.log('payErr');
				console.log(JSON.stringify(ret));
				console.log(JSON.stringify(err));
				api.toast({
					msg: '抱歉，未能正常支付成功，code:' + err.code
				})
			}
			payIng=false;
		},
		//下单提交动作
		payIng = false,
		payThisOrder = function () {

			_$.ajax({
				interface: 'getBuildOrder',
				data: {
					id: api.pageParam.id
				},
				call: function (data, err) {

					if (data.status === 201) {
						api.toast({
							msg: data.msg
						})
					} else if (data.status === 200) {
						var wxPay = api.require('wxPay'),
							orderIntro = data.data;

						if(payIng===false){
							if (__CONFIG__.runtime === 'dev') {
								wxPay.config({
									apiKey: 'wx5684e074db9f6b46',
									mchId: '1496614802',
									partnerKey: 'hkf34eyccrdzdzlqc1bwnnj33vuogd3y',
									notifyUrl: 'https://yikao.baolanbb.com/addons/yi_kao/inc/pay.php'
								}, function (ret, err) {
									if (ret.status) {
										wxPay.pay(orderIntro, payOver);
									} else {
										api.toast({
											msg: '抱歉，未能正常支付成功，code:' + err.code
										})
									}
								});
							} else {
								wxPay.payOrder(orderIntro, payOver);
							}
							payIng=setTimeout(function(){
								payIng=false;
							},10000)
						}else{
							api.toast({
								msg: '您动作太快了，请等待接口响应...'
							})
							return false;
						}
					} else if (data.status === 202) {
						api.toast({
							msg: '课程报名成功'
						})
						getOverPage();
					} else {
						api.toast({
							msg: '抱歉，出现了一个未知错误，请联系管理员'
						})
					}
				}
			})
		},
		appready = function () {
			_$.ajax({
				interface: 'getTcourseDetails',
				data: {
					id: api.pageParam.id
				},
				call: function (data) {
					_$.template('mechanism_order_view_template', {
						data: data.data,
						img: _$.imgCache.default
					}, function (html) {
						_$.sleep(function () {
							$('#mechanism_order_view').html(html);
							_$.imgCache.load();
							$('.skin_btn').fadeIn();
						}, 1000)
					})
				}
			})
		};
</script>
<script src="../../lib/js/config.js"></script>
<script src="../../lib/js/jquery.js"></script>
<script src="../../lib/js/common.js"></script>
<script>

</script>
</body>
</html>